package com.bjb.vr.remote.ws.websocket.websocket;

import com.alibaba.fastjson.JSONObject;
import com.bjb.vr.remote.ws.rabbitmq.constants.MessageActionConstants;
import com.bjb.vr.remote.ws.websocket.entity.Action;
import com.bjb.vr.remote.ws.websocket.entity.BeatTypeParameter;
import com.bjb.vr.remote.ws.websocket.entity.Command;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.Calendar;
import java.util.Map;

/**
 * @version 1.0.0
 * @description:
 * @author: caihaibo
 * @createDate: 2022/6/13
 */
@Slf4j
@Setter
public class StuKeepHeartThread implements Runnable {

    private int heartPeriodSeconds;

    @Override
    public void run() {
        Action action = new Action();
        action.setAction(MessageActionConstants.MESSAGE_ACTION_BEAT_HEART);
        Command command = new Command();
        BeatTypeParameter beatTypeParameter = new BeatTypeParameter();
        beatTypeParameter.setBeatType(MessageActionConstants.SERVER_BEAT_TYPE);
        command.setParameter(beatTypeParameter);
        action.setCommand(command);
        while (true) {
            try {
                Map<String, Map<String, WebSocketServer>> classMap = WebSocketServer.classMap;
//                log.info("StuKeepHeartThread classMap.size() " + classMap.size());
                if(classMap.size()<=0) {
                    Thread.sleep(1000);
                    continue;
                }
                if( heartPeriodSeconds==0 ){
                    heartPeriodSeconds = 8000;
                }
                sendPing(classMap,JSONObject.toJSONString(action));

                Thread.sleep(heartPeriodSeconds*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private  synchronized void sendPing(Map<String, Map<String, WebSocketServer>> classMap,  String message) throws IOException{

        classMap.entrySet().forEach(entry ->{
            Map connectMap = (Map)entry.getValue();
            connectMap.forEach((key,value)->{
                synchronized (value){
                    WebSocketServer server = (WebSocketServer)value;
                        if(!server.isAdmin()) {
                            server.setHeartTime(getTimeInMillis(heartPeriodSeconds-3));
                            server.setHeart(false);
//                            log.info("发送到学生端消息 " + message + " udid " + server.getDeviceId());
                            server.session.sendText(message);
                        }
                }
            });
        });


    }


    private  long getTimeInMillis(int addTime){
        Calendar c = Calendar.getInstance();
        c.set(Calendar.SECOND,c.get(Calendar.SECOND)+addTime);
        return c.getTimeInMillis();
    }

//    public synchronized void sendMsg(String message) throws IOException{
//        if(connections.size()<=0)
//            return;
//        for (WebSocketEntity entity : connections) {
//            synchronized (entity) {
//                ((Session) entity.getSession()).getBasicRemote().sendText(message); // 回复用户
//            }
//        }
//    }

}
